import 'dart:convert';

import 'package:byw_blog/network/NetworkInterface.dart';
import 'package:byw_blog/utils/AppUtil.dart';
import 'package:byw_blog/widget/CommentWidget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:intl/intl.dart';

class ViewRssPage extends StatefulWidget {
  final RssFeed;

  ViewRssPage({Key? key, required this.RssFeed}) : super(key: key);

  @override
  State<ViewRssPage> createState() => _ViewRssPageState(this.RssFeed);
}

class _ViewRssPageState extends State<ViewRssPage> {
  final RssFeed;
  static const String TYPE_COMMENT_ON = "comment_on";
  static const String TYPE_SHARE = "share";

  List<dynamic> comments = [];

  _ViewRssPageState(this.RssFeed);

  @override
  void initState() {
    super.initState();

    getComment();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        title: Text(RssFeed['title']),
        actions: [
          Center(
            child: PopupMenuButton(
              itemBuilder: (BuildContext context) {
                return [
                  const PopupMenuItem(
                    value: TYPE_SHARE,
                    child: Text("分享"),
                  ),
                  const PopupMenuItem(
                    value: TYPE_COMMENT_ON,
                    child: Text("评论"),
                  ),
                ];
              },
              onSelected: (String value) {
                if (value == TYPE_SHARE) {
                  showModalBottomSheet(
                      context: context,
                      builder: (context) {
                        return Container(
                          height: 120,
                          child: Column(
                            children: <Widget>[
                              Container(
                                margin: const EdgeInsets.only(top: 10),
                                child: const Text('分享'),
                              ),
                              const SizedBox(height: 20),
                              Container(
                                padding:
                                    const EdgeInsets.fromLTRB(20, 0, 20, 0),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceAround,
                                  children: [
                                    AppUtil.getImage50('images/qq.png'),
                                    AppUtil.getImage50('images/wechat.png'),
                                  ],
                                ),
                              )
                            ],
                          ),
                        );
                      }).then((value) {
                    print(value);
                  });
                } else {
                  showModalBottomSheet(
                      context: context,
                      builder: (context) {
                        return Scaffold(
                          bottomNavigationBar: inputText(),
                          body: CommentWidget(comments),
                        );
                      });
                }
              },
            ),
          )
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
        child: Stack(
          children: [
            ListView(
              children: [
                Text(
                  RssFeed['title'],
                  style: const TextStyle(
                      fontWeight: FontWeight.bold, fontSize: 20),
                ),
                const SizedBox(
                  height: 10,
                ),
                Row(
                  children: [
                    Text(DateFormat('MMM dd')
                        .format(DateTime.parse(RssFeed['pubDate'].toString()))),
                    Text(RssFeed['author'])
                  ],
                ),
                SingleChildScrollView(
                  child: Html(
                    data: RssFeed['content'],
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  // 声明controller
  TextEditingController emailController = TextEditingController();

  inputText() {
    return Padding(
      padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
      child: ConstrainedBox(
        constraints: const BoxConstraints(maxHeight: 50),
        child: TextField(
          controller: emailController,
          maxLines: 1,
          textAlign: TextAlign.start,
          decoration: InputDecoration(
              suffixIcon: IconButton(
                icon: const Icon(Icons.send),
                onPressed: () {
                  if (emailController.text.isNotEmpty) {
                    NetworkInterface.addComment("ELE-AL00",
                            emailController.text, RssFeed['link'], "0", "0")
                        .then((value) {
                      emailController.text = "";
                      getComment();
                    });
                  } else {
                    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
                      content: Text('评论不能为空'),
                    ));
                  }
                },
              ),
              fillColor: const Color(0x30cccccc),
              filled: true,
              hintText: "请输入..."),
        ),
      ),
    );
  }

  void getComment() {
    NetworkInterface.getComment(RssFeed['link']).then((value) {
      setState(() {
        comments.clear();
        comments.addAll(value);
      });
    });
  }
}
